/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/11/26 22:52
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

(function () {

    "use strict";

    // 显示浮框
    function showCover(type, html) {
        var css = 'thumb-logo-cover', dom = $('<div></div>');
        if (type == 'success') {
            css += '  thumb-color-blue';
        } else {
            css += '  thumb-color-red';
        }
        dom.addClass(css);
        dom.html(html);
        $('#logo-box').append(dom);
    }

    // 隐藏浮框
    function removeCover() {
        var dom = $('#logo-box').find('.thumb-logo-cover');
        dom.fadeOut(3000, function () {
            dom.remove();
        });
    }

    sm.controller('admin-system-setting', ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {

        $scope.showLogoTip = false;

        var api = {
            getSetting: '/system/setting/getSets',
            saveBaseSettingUrl: '/system/setting/saveBaseSetting',
            uploadUrl: '/system/setting/uploadLogo',
            saveApisSettingUrl: '/system/setting/setApis',
            saveContactSettingUrl: '/system/setting/setContact'
        };

        // 初始化基础设置
        $scope.logoUrl = '';
        $scope.baseSetting = {
            logo: '',
            siteName: '',
            beian: '',
            copyright: ''
        };
        $scope.api = {
            'luosimao': '',
            'aliExpress': ''
        };
        $scope.contact = {
            telphone: "",
            phone: "",
            address: "",
            qq: "",
            zipCode: "",
            email: ""
        };

        // GET SETTING
        $http.get(api.getSetting).success(function (res) {
            if (res) {
                var data = res.data;

                if (data.baseSetting){
                    $scope.logoUrl = data.baseSetting.fullPath || '';
                    $scope.baseSetting.logo = data.baseSetting.logo || '';
                    $scope.baseSetting.siteName = data.baseSetting.siteName || '';
                    $scope.baseSetting.beian = data.baseSetting.beian || '';
                    $scope.baseSetting.copyright = data.baseSetting.copyright || '';
                }

                if (data.contact) {
                    $scope.contact.telphone = data.contact.telphone || "";
                    $scope.contact.phone = data.contact.phone || "";
                    $scope.contact.qq = data.contact.qq || "";
                    $scope.contact.email = data.contact.email || "";
                    $scope.contact.address = data.contact.address || "";
                    $scope.contact.zipCode = data.contact.zipCode || "";
                }
            }
        });


        // =============================================
        var $ = jQuery,
            ratio = window.devicePixelRatio || 1,
            uploader;

        // 初始化Web Uploader
        uploader = WebUploader.create({
            auto: true,
            server: api.uploadUrl,
            pick: '#uploader',
            swf: '/assets/library/webuploader/Uploader.swf',
            duplicate: true,
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,png'
            }
        });

        // 上传之前
        uploader.on( 'uploadSuccess', function(file, res) {
            if (res.state) {
                $scope.baseSetting.logo = res.data.key;
                $scope.logoUrl = res.data.fullPath;
            } else {
                var error = res.error ? res.error : '上传失败';
                Modal.error(error);
            }
        });

        uploader.on( 'uploadComplete', function( file ) {
            $scope.$apply();
        });
        // ===========end==================================

        $scope.saveBaseSetting = function () {
            $http.post(api.saveBaseSettingUrl, $scope.baseSetting).success(function (res) {
                afterPost(res);
            });
        };

        $scope.saveApis = function () {
            $http.post(api.saveApisSettingUrl, $scope.api).success(function (res) {
                afterPost(res);
            });
        };

        $scope.saveContact = function () {
            $http.post(api.saveContactSettingUrl, $scope.contact).success(function (res) {
                afterPost(res);
            });
        };

        function afterPost(res) {
            if (res.state) {
                Modal.success('设置成功');
            } else {
                var error = res.error ? res.error : '设置失败';
                Modal.error(error);
            }
        }

    }]);


})();